package JAVACORE_TRAINING.OOPS_CONCEPTS.ARRAY_AND_COLLECTIONS.DYNAMIC_ARRAYS.SAMPLES.SP03;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Models {
	/***
	 * 
	 * @param lisStudent
	 * @param shOpts = 0: Mark ASC; shOpts = 1: Mark DESC;
	 * @return
	 */
	public List<StudentObject> SortElements(
					List<StudentObject> lisStudent, short shOpts){
		List<StudentObject> lisSTemp = null;	
		switch (shOpts) {
			/* Mark ASC with Bubbdy Sort */
			case 0:  lisSTemp = BUBBLESORT(lisStudent); break;			
			/* Mark ASC with Insertion Sort */
			case 1:  lisSTemp = INSERTIONSORT(lisStudent); break;			
			/* Mark ASC with Selection Sort */
			case 2:  lisSTemp = SELECTIONSORT(lisStudent); break;			
			/* Mark ASC with  Sort */
			case 3:  lisSTemp = ITEMSORT(lisStudent); break;
		}
		return lisSTemp;
	}
	
	private List<StudentObject> BUBBLESORT(List<StudentObject> lisStudent) {
		List<StudentObject> lisSTemp  = new ArrayList<>(lisStudent);
		// lisTemp.addAll(lisD);
		for (int i = 0; i < lisSTemp.size(); i++) {
			for (int j = i + 1; j < lisSTemp.size(); j++) {
				if (lisSTemp.get(i).getMark() > lisSTemp.get(j).getMark()) {
					StudentObject objTemp = lisSTemp.get(i);  //StudentObject at i index
					lisSTemp.set(i, lisSTemp.get(j));
					lisSTemp.set(j, objTemp);
				}
			}
		}
		return lisSTemp;
	}

	private List<StudentObject> INSERTIONSORT(List<StudentObject> lisStudent) {
		List<StudentObject> lisSTemp = new ArrayList<>(lisStudent);
		int i, j;
		StudentObject objStudent;
		for (j = 1; j < lisSTemp.size(); j++) {
			objStudent = lisSTemp.get(j);
			for (i = j - 1; (i >= 0) && 
									(lisSTemp.get(i).getMark() > objStudent.getMark()); i--)
				lisSTemp.set(i + 1, lisSTemp.get(i));
			lisSTemp.set(i + 1, objStudent);
		}
		return lisSTemp;
	}
	
	private List<StudentObject> SELECTIONSORT(List<StudentObject> lisStudent){
		List<StudentObject> lisSTemp = new ArrayList<>(lisStudent);
		 int i, j, first;
		 StudentObject objStudent;
	        for (i = lisSTemp.size() - 1; i > 0; i--) {
	            first = 0; 
	            for (j = 1; j <= i; j++) 
	            {
	                if (lisSTemp.get(j).getMark() > lisSTemp.get(first).getMark())
	                    first = j;	              
	            }
	            objStudent = lisSTemp.get(first); 
	            lisSTemp.set(first, lisSTemp.get(i));
	            lisSTemp.set(i, objStudent);
	        }
	        return lisSTemp;
	}
	
	private List<StudentObject> ITEMSORT(List<StudentObject> lisStudent){
		List<StudentObject> lisSTemp = new ArrayList<>(lisStudent);
		Collections.sort(lisSTemp, new Comparator<StudentObject>() {
            @Override
            public int compare(StudentObject obj01, StudentObject obj02) {            	              
                if(obj01.getMark() < obj02.getMark()){
                	return 1;
                }
                return -1;
            }
		});
		return lisSTemp;
	}
}
